package com.scfire.job

import java.text.DateFormat
import java.text.SimpleDateFormat

import com.scfire.message.SystemMessage
import com.scfire.project.Payment
import com.scfire.project.Project
import com.scfire.project.ProjectClosing
import com.scfire.project.ProjectFeedback
import com.scfire.security.Member
import com.scfire.util.DateUtil
import com.scfire.weixin.WeiXinConstants

/** 
 * Copyright 2015 Shudao Info Tech Co. Ltd.  All rights reserved.
 * @author lhzeng
 * @email zenglh@haoshudao.com
 * @date 2015年9月7日 上午11:26:44 
 * @version V1.0
 */


/**
 * 
 *核查火堆项目是否已过期定时任务Job
 *
 */
class CheckProjectMessageJob {
	def mongo
	def grailsApplication
	def weiXinService
	
	//设置定时器
	static triggers = {
		//定时器任务，每天凌晨0:00跑一次
		cron name:'CheckProjectMessageJob', startDelay:10000, cronExpression: '0 26 15 22 * ?'	
	 }
	//代码执行区
	def execute() {
		
		println "=========Start Check CheckProjectMessage Job Run!========="+new Date()
		//查询正在执行中的项目
		List<Project> projects = Project.findAllByVerified("doing")
		DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(Project project:projects){
			boolean flag= true
			def payment = Payment.findByProject(project.id)
			def ProjectClosing = ProjectClosing.findByProject(project.id)
			//提交结项报告的不提示
			if(ProjectClosing){
				flag= false
				continue;
			}
			//def ceckFeedback = SystemMessage.findByProjectAndType(project.id,"forFeedBack")
			//if(!ceckFeedback){
				List<ProjectFeedback> projectfeedbacks = ProjectFeedback.findAllByProjectAndFeedbackType(project.id,"fback")
				if(projectfeedbacks){
					for (int i=0;i<list.size();i++) {
						int j=DateUtil.getDoubleMargin(fmt.format(new Date()),fmt.format(payment.date))
						if(j>30){
							if(projectfeedbacks.get(i).date>payment.date){
								flag=false;
								break;
							}
						}
					}
				}else{
					int j=DateUtil.getDoubleMargin(fmt.format(new Date()),fmt.format(payment.date))
					if(j>30){
						flag=true;
					}
				}
				if(flag){
					String s="定期分享项目进展可以让更多用户了解和支持你的善举，赶快点击“详情”进入页面分享吧。"
					SystemMessage systemMessage = new SystemMessage();
					systemMessage.project=project.id
					systemMessage.member= project.member
					systemMessage.date=new Date()
					systemMessage.type="forFeedBack"
					systemMessage.message="执行项目30日内无反馈提醒"
					systemMessage.save(flush:true)
					def member = Member.findById(project.member)
					weiXinService.template_status(WeiXinConstants.service_id, member.openid, "亲爱的"+member.nickname+"别忘了定期分享您正在执行中的项目进展", fmt.format(new Date()), "项目进展", "执行中", s , WeiXinConstants.server+"/project/createProjectCloseing?projectid="+project?.id)
				}
			//}
		}
		println "=========End  Check CheckProjectMessage Job Run!========="+new Date()
	
	
		
	}

}
 